package com.ice.work.moment.user.filter;

import com.ice.work.moment.facade.user.filter.AbstractResourceFilter;
import com.ice.work.moment.facade.user.model.Resource;
import com.ice.work.moment.user.service.resource.IResourceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

import java.util.List;
import java.util.Map;

/**
 * @Description 资源
 * @Author DELL
 * @Create 2019-02-15
 * @Since 1.0.0
 */
@Slf4j
@Component
public class ResourceFilter extends AbstractResourceFilter implements InitializingBean {
    @Autowired
    private RequestMappingHandlerMapping requestMappingHandlerMapping;

    @Autowired
    private IResourceService resourceService;


    @Override
    public void afterPropertiesSet() {
        Map<RequestMappingInfo, HandlerMethod> requestMappingInfoMap = requestMappingHandlerMapping.getHandlerMethods();
        List<Resource> resourceList = getResources(requestMappingInfoMap);
        if (!CollectionUtils.isEmpty(resourceList)) {
            resourceService.saveOrUpdateBySign(resourceList);
        }
    }
}
